// 这份代码专门处理逻辑层的操作
const wordDao = require('../dao/wordDao');

const wordService = {
    /*
     * 获取 20个 待背诵单词
     */
    async getAnyWord(uid) {
        let result;
        try {
            let wordList = await wordDao.getAnyWord(uid);
            result = { success: true, msg: '获取待背诵单词成功', data: { wordList } };
        } catch (err) {
            console.log(err);
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 记录背诵进度
     */
    async saveUserProgress(uid, progress, time) {
        let result;
        try {
            await wordDao.saveUserProgress(uid, progress, time);
            result = { success: true, msg: '记录背诵进度成功' };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    // 获取用户背诵进度
    async getUserProgress(uid) {
        let result;
        try {
            let data = await wordDao.getUserProgress(uid);
            result = { success: true, msg: '获取单词背诵进度成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    // 加载这个单词
    async loadThisWord(wordId) {
        let result;
        try {
            let data = await wordDao.loadThisWord(wordId);
            result = { success: true, msg: '获取单词成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    }
};

module.exports = wordService;
